package com.cdtye.itps.cms.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public final class PythonUtil {

    private static final Logger log = LoggerFactory.getLogger(PythonUtil.class);

    public static void main(String[] args) {

        String result = transfer("python", "D:/python/mileage_correction.py", "D:/python/test2020-06-09.xls");
        System.out.println(result);
    }

    public static String transfer(String py, String pyName, String path) {
        log.error(pyName + "====" + path);
        //缓存写入流
        BufferedReader in=null;
        try {
            String[] args1 = new String[]{ py, pyName, path};
            // String[] args2 = new String[]{ "python", ""}
            Process proc = Runtime.getRuntime().exec(args1);//执行py文件
            in = new BufferedReader(new InputStreamReader(proc.getInputStream(), "UTF-8"));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }
            proc.waitFor();
//            System.out.println(sb.toString());
            log.error("PythonUtil = " + sb.toString());
            return sb.toString();
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

}
